package com.example.mywanandroid;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.example.mywanandroid.home.HomeFragment;
import com.example.mywanandroid.knowledge_hierarchy.KnowledgeHierarchyFragment;
import com.example.mywanandroid.navigation.NavigationFragment;
import com.example.mywanandroid.project.ProjectFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private KnowledgeHierarchyFragment knowledgeHierarchyFragment;
    private NavigationFragment navigationFragment;
    private ProjectFragment projectFragment;
    private Fragment[] fragments;
    private int lastFragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 默认的方式，不带缓存的fragment
        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.menu_home, R.id.menu_knowledge_hierarchy, R.id.menu_navigation, R.id.menu_project
        ).build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController); // 使用replace的方法替换Fragment

        // 自定义的导航栏，带缓存的fragment
//        homeFragment = new HomeFragment();
//        knowledgeHierarchyFragment = new KnowledgeHierarchyFragment();
//        navigationFragment = new NavigationFragment();
//        projectFragment = new ProjectFragment();
//        fragments = new Fragment[]{homeFragment, knowledgeHierarchyFragment, navigationFragment, projectFragment};
//        // 设置fragment到布局
//        getSupportFragmentManager().beginTransaction()
//                .replace(R.id.nav_host_fragment, homeFragment)
//                .show(homeFragment).commit();
//        bottomNavigationView = findViewById(R.id.nav_view);
//
//        // 这里是bottomNavigationView的点击事件
//        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemReselectedListener);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemReselectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_home: {
                    if (lastFragment != 0) {
                        switchFragment(lastFragment, 0);
                        lastFragment = 0;
                    }
                    return true;
                }
                case R.id.menu_knowledge_hierarchy: {
                    if (lastFragment != 1) {
                        switchFragment(lastFragment, 1);
                        lastFragment = 1;
                    }
                    return true;
                }
                case R.id.menu_navigation: {
                    if (lastFragment != 2) {
                        switchFragment(lastFragment, 2);
                        lastFragment = 2;
                    }
                    return true;
                }
                case R.id.menu_project: {
                    if (lastFragment != 3) {
                        switchFragment(lastFragment, 3);
                        lastFragment = 3;
                    }
                    return true;
                }
                default:
                    break;
            }
            return false;
        }
    };

    // 切换fragment
    private void switchFragment(int lastFragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 隐藏上一个Fragment
        transaction.hide(fragments[lastFragment]);
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.nav_host_fragment, fragments[index]);
        }
        transaction.show(fragments[index]).commit();
    }
}